package com.linkoog.devtools.websocket.toolwindow.left;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.project.Project;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import com.intellij.util.ui.table.TableModelEditor;
import com.linkoog.devtools.websocket.model.WSFrameData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WSDataTableUtil {


    private final static Color color = new Color(234, 240 ,255);

    public static JBTable initLeft_Table(Project project){
        ListTableModel<WSFrameData> model = new ListTableModel<>(getColumns(project));
        FilteringTableModel<WSFrameData> filteringTableModel = new FilteringTableModel<>(model, WSFrameData.class);
        JBTable jbTable = new JBTable(filteringTableModel);
        //model.setFilter(myFilter);
        // 创建表头渲染器
        DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
        headerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

        // 设置表头渲染器
        jbTable.getTableHeader().setDefaultRenderer(headerRenderer);

        //RowSorter<ListTableModel<WSFrameData>> sorter = new TableRowSorter<>(model);
        //jbTable.setRowSorter(sorter);

        jbTable.getTableHeader().setReorderingAllowed(false);
        int columnCount = model.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            final String columnName = model.getColumnName(i);
            TableColumn column = jbTable.getColumn(columnName);

            if ("Type".equals(columnName)){
                int width = 55;
                column.setWidth(width);
                column.setMinWidth(width);
                column.setMaxWidth(width);
                column.setPreferredWidth(width);
                column.setResizable(false);



                DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){

                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        final int selectedRow = table.getSelectedRow();
                        if ( selectedRow == row) {
                            super.setForeground(table.getSelectionForeground());
                            super.setBackground(table.getSelectionBackground());
                        } else {
                            final WSFrameData item = model.getItem(row);
                            if (item.getType() == 1){
                                super.setForeground(table.getForeground());
                                super.setBackground(color);
                            } else {
                                super.setForeground(table.getForeground());
                                super.setBackground(table.getBackground());
                            }
                        }



                        this.setFont(table.getFont());
                        if (value instanceof Integer){
                            Integer integer = (Integer)value;
                            setIcon(integer == 0 ? AllIcons.Ide.OutgoingChangesOn : AllIcons.Ide.IncomingChangesOn);
                        }

                        return this;
                    }
                };
                renderer.setHorizontalAlignment(SwingConstants.CENTER);
                column.setCellRenderer(renderer);
            }

            if ("Time".equals(columnName)){
                int width = 102;
                column.setWidth(width);
                column.setMinWidth(width);
                column.setMaxWidth(width);
                column.setPreferredWidth(width);
                column.setResizable(false);

                DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                        final int selectedRow = table.getSelectedRow();
                        if ( selectedRow == row) {
                            super.setForeground(table.getSelectionForeground());
                            super.setBackground(table.getSelectionBackground());
                        } else {
                            final WSFrameData item = model.getItem(row);
                            if (item.getType() == 1){
                                super.setForeground(table.getForeground());
                                super.setBackground(color);
                            } else {
                                super.setForeground(table.getForeground());
                                super.setBackground(table.getBackground());
                            }
                        }

                        return this;
                    }
                };
                renderer.setHorizontalAlignment(SwingConstants.CENTER);
                column.setCellRenderer(renderer);
            }

            if ("Data".equals(columnName)){
                DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                        final int selectedRow = table.getSelectedRow();
                        if ( selectedRow == row) {
                            super.setForeground(table.getSelectionForeground());
                            super.setBackground(table.getSelectionBackground());
                        } else {
                            final WSFrameData item = model.getItem(row);
                            if (item.getType() == 1){
                                super.setForeground(table.getForeground());
                                super.setBackground(color);
                            } else {
                                super.setForeground(table.getForeground());
                                super.setBackground(table.getBackground());
                            }
                        }


                        return this;
                    }
                };
                column.setCellRenderer(renderer);
                column.setCellEditor(new EditorExTableCellEditor(project));
            }

        }

        return jbTable;
    }

    private static ColumnInfo[] getColumns(Project project){
        return new ColumnInfo[]{
                new TableModelEditor.EditableColumnInfo<WSFrameData, Integer>("Type") {
                    @NotNull
                    @Override
                    public Integer valueOf(WSFrameData item) {
                        return item.getType();
                    }

                    @Override
                    public boolean isCellEditable(WSFrameData item) {
                        return false;
                    }

                    @Override
                    public int getWidth(JTable table) {
                        return 60;
                    }

                    @Override
                    public TableCellRenderer getRenderer(WSFrameData item) {
                        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                        renderer.setHorizontalAlignment(SwingConstants.CENTER);
                        return renderer;
                    }
                },
                new TableModelEditor.EditableColumnInfo<WSFrameData, String>("Time") {
                    @NotNull
                    @Override
                    public String valueOf(WSFrameData item) {
                        return formatDate(item.getTime());
                    }

                    @Override
                    public boolean isCellEditable(WSFrameData item) {
                        return false;
                    }

                    @Override
                    public int getWidth(JTable table) {
                        return 60;
                    }
                },
                new TableModelEditor.EditableColumnInfo<WSFrameData, String>("Data") {
                    @NotNull
                    @Override
                    public String valueOf(WSFrameData item) {
                        return item.getData();
                    }

                    @Override
                    public boolean isCellEditable(WSFrameData item) {
                        return true;
                    }

                    @Override
                    public int getWidth(JTable table) {
                        return -1;
                    }

                    @Override
                    public @Nullable TableCellEditor getEditor(WSFrameData wsFrameData) {
                        return new EditorExTableCellEditor(project);
                    }
                }
        };
    }

    private static String formatDate(Date date){
        try {
            return new SimpleDateFormat("HH:mm:ss.SSS").format(date);
        }catch (Exception ignored){
        }
        return "";
    }


}
